package me.zhengjie.modules.equipment.service.impl;

import me.zhengjie.modules.equipment.domain.TSparepart;
import me.zhengjie.modules.equipment.domain.TSparepartRetrieval;
import me.zhengjie.modules.equipment.repository.TSparepartRepository;
import me.zhengjie.modules.system.common.EnumSparepartStatus;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.modules.equipment.repository.TSparepartRetrievalRepository;
import me.zhengjie.modules.equipment.service.TSparepartRetrievalService;
import me.zhengjie.modules.equipment.service.dto.TSparepartRetrievalDTO;
import me.zhengjie.modules.equipment.service.dto.TSparepartRetrievalQueryCriteria;
import me.zhengjie.modules.equipment.service.mapper.TSparepartRetrievalMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;

/**
* @author jie
* @date 2019-11-16
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TSparepartRetrievalServiceImpl implements TSparepartRetrievalService {

    @Autowired
    private TSparepartRetrievalRepository tSparepartRetrievalRepository;

    @Autowired
    private TSparepartRepository tSparepartRepository;

    @Autowired
    private TSparepartRetrievalMapper tSparepartRetrievalMapper;

    @Override
    public Object queryAll(TSparepartRetrievalQueryCriteria criteria, Pageable pageable){
        Page<TSparepartRetrieval> page = tSparepartRetrievalRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(tSparepartRetrievalMapper::toDto));
    }

    @Override
    public Object queryAll(TSparepartRetrievalQueryCriteria criteria){
        return tSparepartRetrievalMapper.toDto(tSparepartRetrievalRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public TSparepartRetrievalDTO findById(Integer id) {
        Optional<TSparepartRetrieval> tSparepartRetrieval = tSparepartRetrievalRepository.findById(id);
        ValidationUtil.isNull(tSparepartRetrieval,"TSparepartRetrieval","id",id);
        return tSparepartRetrievalMapper.toDto(tSparepartRetrieval.get());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TSparepartRetrievalDTO create(TSparepartRetrieval resources) {
        return tSparepartRetrievalMapper.toDto(tSparepartRetrievalRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TSparepartRetrieval resources) {
        Optional<TSparepartRetrieval> optionalTSparepartRetrieval = tSparepartRetrievalRepository.findById(resources.getId());
        ValidationUtil.isNull( optionalTSparepartRetrieval,"TSparepartRetrieval","id",resources.getId());

        TSparepartRetrieval tSparepartRetrieval = optionalTSparepartRetrieval.get();
        // 此处需自己修改
        resources.setId(tSparepartRetrieval.getId());
        tSparepartRetrievalRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer id) {
        tSparepartRetrievalRepository.deleteById(id);
    }

    @Override
    public TSparepartRetrievalDTO log(TSparepartRetrieval resources) {
        Optional<TSparepart> optionalTSparepart = tSparepartRepository.findById(resources.getEqptId());
        ValidationUtil.isNull( optionalTSparepart,"备件信息","id",resources.getId());

        TSparepart tSparepart = optionalTSparepart.get();

        tSparepart.setState(resources.getState());
        tSparepartRepository.save(tSparepart);

        resources.setRetrievalBy(SecurityUtils.getName());
        resources.setRetrievalTime(new Timestamp(System.currentTimeMillis()));
        return tSparepartRetrievalMapper.toDto(tSparepartRetrievalRepository.save(resources));
    }
}